package app.beit.fintrack.controller;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import app.beit.fintrack.utils.constr.MsgConstr;

public class BaseController {
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected ResponseFintrack responseError(final String code, final String msg) {
		final ResponseFintrack response = new ResponseFintrack();
		response.setCode(code);
		response.setMsg(msg);
		response.setSuccess(false);
		response.setDatas(null);
		return response;
	}

	@SuppressWarnings({ "rawtypes"})
	protected ResponseFintrack responseSuccess() {
		final ResponseFintrack response = new ResponseFintrack();
		response.setCode(MsgConstr.SUCCESS_CODE);
		response.setMsg(MsgConstr.SUCCESS_MSG);
		response.setSuccess(true);
		
		return response;
	}

	@SuppressWarnings({ "rawtypes", "unchecked"})
	protected ResponseFintrack responseSuccessNullData() {
		final ResponseFintrack response = new ResponseFintrack();
		response.setCode(MsgConstr.SUCCESS_NULL_DATA_CODE);
		response.setMsg(MsgConstr.SUCCESS_NULL_DATA_MSG);
		response.setSuccess(true);
		response.setDatas(null);
		
		return response;
	}
	
	protected Object getAttributeInSession(final HttpServletRequest request, final String name) {
		return request.getSession().getAttribute(name);
	}
	
	protected void setAttributeInSession(final HttpServletRequest request, final String sessionName, final Object obj) {
		request.getSession().setAttribute(sessionName, obj);
	}
	
	@SuppressWarnings("rawtypes")
	protected void removeAllSessionAttributes(final HttpServletRequest request) {
		final Enumeration names = request.getSession().getAttributeNames();
		while (names.hasMoreElements()) {
			String attrname = (String) names.nextElement();
			request.getSession().removeAttribute(attrname);
		}
	}
}
